﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Data;
using System.Data.SqlClient;
using GPSTracker.Helper;
using System.Text;

namespace GPSTracker.Views.DAL
{
    public class TempoDAL
    {
        public DataTable FillTempoMasterGrid(string strTempoID)
        {
            var sb = new StringBuilder();
            var dt = new DataTable();

            if (strTempoID == string.Empty)
            {
                var strSQL = "SELECT * FROM VW_GetTempoMaster WHERE IsActive = 1";
                var da = new SqlDataAdapter(strSQL, CommonFx.GetSqlCon());
                da.Fill(dt);
            }
            else
            {
                sb.Append("SELECT * FROM VW_GetTempoMaster WHERE TempoID = '" + strTempoID + "' ");
                var da = new SqlDataAdapter(sb.ToString(), CommonFx.GetSqlCon());
                da.Fill(dt);
            }

            return dt;
        }

        public DataTable FillWarehouse(string strWarehouseID)
        {
            var sb = new StringBuilder();
            var dt = new DataTable();

            if (strWarehouseID == string.Empty)
            {
                var strSQL = "SELECT * FROM VW_GetWarehouse";
                var da = new SqlDataAdapter(strSQL, CommonFx.GetSqlCon());
                da.Fill(dt);
            }
            else
            {
                var strSQL = "SELECT * FROM VW_GetWarehouse";
                var da = new SqlDataAdapter(strSQL, CommonFx.GetSqlCon());
                da.Fill(dt);
            }

            return dt;
        }

        public DataTable FillDriver(string strWarehouseID,bool IsNewMode)
        {
            var sb = new StringBuilder();
            var dt = new DataTable();

            if (IsNewMode == true)
            {
                var strSQL = "SELECT LoginID,EmpName FROM dbo.VW_EmployeeMaster WHERE WarehouseID = '" + strWarehouseID + "' AND IsActive = 1 AND Designation='Tempo Driver'";
                var da = new SqlDataAdapter(strSQL, CommonFx.GetSqlCon());
                da.Fill(dt);
            }
            else
            {
                var strSQL = "SELECT LoginID,EmpName FROM dbo.VW_EmployeeMaster WHERE WarehouseID = '" + strWarehouseID + "' AND Designation='Tempo Driver'";
                var da = new SqlDataAdapter(strSQL, CommonFx.GetSqlCon());
                da.Fill(dt);
            }

            return dt;
        }

        public void DeleteTempoMaster(string strTempoID)
        {
            var sb = new StringBuilder();
            sb.Append("UPDATE dbo.M_Tempo SET IsActive = 0 WHERE TempoID = '" + strTempoID + "' ");

            var cmd = new SqlCommand(sb.ToString(), CommonFx.GetSqlCon());
            cmd.ExecuteNonQuery();
        }
    }
}
